package com.devilwwj.http.core;

import android.os.Build;

import com.devilwwj.http.callback.OnGlobalExceptionListener;
import com.devilwwj.http.entities.FileEntity;
import com.devilwwj.http.exception.HttpException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * Created by devilwwj on 15/7/15.
 */
public class Request {



    public enum RequestMethod {
        GET, POST, PUT, DELETE;

    }
    // 请求地址
    public String url;
    // 请求方法
    public RequestMethod method;

    // header信息
    public Map<String,String> headers;
    // 请求实体
    public String content;

    // 回调接口
    public ICallback iCallback;

    // 进度更新的开关
    public boolean enableProgressUpdated = false;

    // 异常回调接口
    public OnGlobalExceptionListener onGlobalExceptionListener;

    // 最大重试次数,这里为3次
    public int maxRetryCount = 3;

    // 是否取消操作
    public volatile boolean isCancelled;

    // 用来标识activity的tag
    public String tag;

    // 封装AsyncTask
    public RequestAsyncTask requestAsyncTask;

    // 状态
    public static final int STATE_UPLOAD = 1;
    public static final int STATE_DOWNLOAD = 2;

    public String filePath;
    public ArrayList<FileEntity> fileEntities;


    /**
     * 默认构造函数，默认为GET请求
     * @param url
     */
    public Request(String url) {
        this.url = url;
        this.method = RequestMethod.GET;
    }

    /**
     * 构造方法
     * @param url 请求url
     * @param method 请求方法
     */
    public Request(String url, RequestMethod method) {
        this.url = url;
        this.method = method;
    }

    /**
     * 设置回调接口
     * @param iCallback
     */
    public void setCallback(ICallback iCallback) {
        this.iCallback = iCallback;
    }


    /**
     * 设置是否可进行进度更新
     * @param enableProgressUpdated
     */
    public void setEnableProgressUpdated(boolean enableProgressUpdated) {
        this.enableProgressUpdated = enableProgressUpdated;
    }


    /**
     * 设置全局异常接口
     * @param onGlobalExceptionListener
     */
    public void setGlobalExceptionListener(OnGlobalExceptionListener onGlobalExceptionListener) {
        this.onGlobalExceptionListener = onGlobalExceptionListener;
    }


    /**
     * 取消请求
     * @param force
     */
    public void cancel(boolean force) {
        this.isCancelled = true;
        iCallback.cancel();

        if (force && requestAsyncTask != null) {
            requestAsyncTask.cancel(force);
        }
    }

    /**
     * 检查是否已经取消请求并抛出异常
     * @throws HttpException
     */
    public void checkIfIsCancelled() throws HttpException {
        if (isCancelled) {
            throw new HttpException(HttpException.ErrorType.CANCEL, "the request has been canceled");
        }
    }

    /**
     * 设置tag
     * @param tag
     */
    public void setTag(String tag) {
        this.tag = tag;
    }


    /**
     * 执行异步请求
     * @param mExecutors
     */
    public void execute(ExecutorService mExecutors) {
       requestAsyncTask = new RequestAsyncTask(this);
       // sdk版本11以上
       if (Build.VERSION.SDK_INT > 11) {
           requestAsyncTask.executeOnExecutor(mExecutors);
       } else {
           requestAsyncTask.execute();
       }
    }


    public void addHeader(String key, String value) {
        if (headers == null){
            headers = new HashMap<String,String>();
        }
        headers.put(key,value);
    }
}
